package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.common.query.DestinationQuery;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/destination")
public class DestinationController {
    @Reference
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") DestinationQuery qo, Model model){
        model.addAttribute("pageInfo",destinationService.query(qo));
        Long parentId = qo.getParentId();
        if (parentId > -1) {
            model.addAttribute("toasts", destinationService.listToasts(parentId));
        }

        return "destination/list";
    }

    @ResponseBody
    @RequestMapping("/setInfo")
    public Object setInfo(Destination destination) {
        AjaxResult result = new AjaxResult();
        try {
            destinationService.setInfo(destination);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/changeHotState")
    public Object changeHotState(Long id, Integer hot) {
        AjaxResult result = new AjaxResult();
        try {
            destinationService.changeHotState(id, hot);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result;
    }

}
